package com.astrob.adapters;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.astrob.model.LonLat;
import com.astrob.nhcmap.R;

import java.util.List;

public class SearchAroundItemAdapter extends BaseAdapter {
	static public class POIDesc {
		public String name;
		public String addr;
		public String phone;
		public String direct;
		public String distance;
		public int index;
		public LonLat pos;
		public boolean showIcon;

		public POIDesc() {
			showIcon = false;
		}
	}

	private List<POIDesc> mPois = null;
	Context mContext;

	public SearchAroundItemAdapter(Context context) {
		mContext = context;
	}

	public void setDataList(List<POIDesc> pois) {
		mPois = pois;
	}
	
	public void clear(){
		if (mPois != null) {
			mPois.clear();
		}		
	}

	@Override
	public int getCount() {
		if (mPois == null) {
			return 0;
		} else {
			return mPois.size();
		}
	}

	@Override
	public Object getItem(int position) {
		if (mPois == null) {
			return null;
		} else {
			return mPois.get(position);
		}
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder;
		if (null == convertView) {
			convertView = LinearLayout.inflate(mContext,
					R.layout.adapter_aroundsearch_item, null);

			holder = new ViewHolder();

			holder.tv_addr = (TextView) convertView
					.findViewById(R.id.aroundsearch_addr);
			holder.tv_name = (TextView) convertView
					.findViewById(R.id.aroundsearch_name);
			holder.tv_name2 = (TextView) convertView
					.findViewById(R.id.aroundsearch_name2);
			holder.tv_direct = (TextView) convertView
					.findViewById(R.id.aroundsearch_direct);
			holder.tv_distance = (TextView) convertView
					.findViewById(R.id.aroundsearch_distance);

			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		POIDesc poi = mPois.get(position);
		if (poi.showIcon) {
			holder.iv_image.setVisibility(View.VISIBLE);
		} else {
			holder.iv_image.setVisibility(View.GONE);
		}

		String[] names = poi.name.split("\n");
		if (names.length > 1) {
			holder.tv_name.setText(names[0]);
			holder.tv_name2.setText(names[1]);
			holder.tv_name2.setVisibility(View.VISIBLE);
		} else {
			holder.tv_name.setText(poi.name);
			holder.tv_name2.setVisibility(View.GONE);
		}

		if (poi.addr != null && poi.addr.length() > 1) {
			holder.tv_addr.setText(poi.addr);
			holder.tv_addr.setVisibility(View.VISIBLE);
		} else {
			holder.tv_addr.setVisibility(View.GONE);
		}

		holder.tv_direct.setText(poi.direct);
		holder.tv_distance.setText(poi.distance);

		return convertView;
	}

	static class ViewHolder {
		ImageView iv_image;
		TextView tv_name;
		TextView tv_name2;
		TextView tv_addr;
		TextView tv_direct;
		TextView tv_distance;
	}
}
